package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.structure.module.activityM.manager.slots.view.scroll.multiList.FilterUtil;
	import code.tools.method.CommonMethod;
	import code.untils.LoaderTypeConst;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 *
	 *@author wenqiang.deng
	 *Create time:2013-5-30 下午6:19:19
	 */
	public class SimpleIcon extends Sprite
	{
		private var _path:String;
		private var _fileType:String;
		//是否位图化icon
		private var _isBitmap:Boolean;
		//比例		
		private var _scale:Number;
		//icon的容器
		private var _conIcon:Sprite;
		private var _tfCount:TextField;
		private var _isMc:Boolean;
		public function SimpleIcon()
		{
			_path = '';
			_fileType = '';
			_isBitmap = false;
			_scale = 1;
			_conIcon = new Sprite();
			this.addChild(_conIcon);
			this.mouseChildren = false;
		}
		
		public function setIconInfo(path:String,subPath:String = 'ICON',_fileType:String = 'swf',isMc:Boolean = false):void
		{
			clearIcon();
			if(path!=null && path!='')
			{
				_isMc = isMc;
				_path = path;
				DllLoaderManager.getInstance().getMaterial(_path,subPath,loadCompleteHandler,_fileType,LoaderTypeConst.LOADER_TYPE_ICON);
			}
		}
		
		/**
		 * 清理icon 
		 * 
		 */		
		public function clear():void
		{
			clearIcon();
			if(_tfCount!=null) _tfCount.visible = false;
		}
		
		/**
		 * 是否把加载到的icon转换为位图 
		 * @param bool
		 * 
		 */		
		public function setBitmap(bool:Boolean):void
		{
			_isBitmap = bool;	
		}
		
		/**
		 * 设置 缩放比例
		 * @param scale
		 * 
		 */		
		public function setScale(scale:Number):void
		{
			_scale = scale;
		}
		
		/**
		 * 显示数量 
		 * @param count
		 * 
		 */		
		public function showCount(count:int):void
		{
			if(_tfCount == null)createCountTf();
			_tfCount.visible = true;
			_tfCount.text = 'x' + count;
		}
		
		/**
		 * 显示数量对比 
		 * @param count1
		 * @param count2
		 * 
		 */		
		public function showCountContrast(count1:int,count2:int):void
		{
			if(_tfCount == null)createCountTf();
			_tfCount.visible = true;
			_tfCount.text = count1 + '/' + count2;
			var fmt:TextFormat = new TextFormat();
			fmt.color = count1 >= count2 ? 0x4F8D0D : 0xF45600;
			_tfCount.setTextFormat(fmt);
		}
		
		/**
		 * 隐藏数量 
		 * 
		 */		
		public function hideCount():void
		{
			_tfCount.visible = false;
		}
		
		/**
		 * 阴影滤镜 
		 * 
		 */		
		public function shadow():void
		{
			this.filters = [FilterUtil.grayGlowFilter];
		}
		
		private function createCountTf():void
		{
			_tfCount = Helpers.FormatTextField(this,-50,20,100,TextFieldAutoSize.NONE,0x4F8D0D,FontManager.getInstance().embeddedFontCName,18,TextFormatAlign.RIGHT);
			_tfCount.filters = [new GlowFilter(0xffffff,1,5,5,10)];
			(_tfCount as AutoTextField).openZoom(100,30);
		}
		
		private function clearIcon():void
		{
			_path = '';
			_fileType = '';
			var len:int = _conIcon.numChildren;
			for(var i:int = 0;i<len;i++)
				_conIcon.removeChildAt(0);
		}
		
		private function loadCompleteHandler(c:*,key:String = ""):void
		{
			if(_path == '' || key.indexOf(_path)==-1||c == null)
				return;
			var icon:*;
			if(_isMc)
				icon = Helpers.drawMc(c,icon);
			else
				icon = Helpers.drawBmd(c,icon);
			icon.mouseEnabled = false; 
			icon.scaleX = _scale;
			icon.scaleY = _scale;
			icon.x = -icon.width/2;
			icon.y = -icon.height/2;
			if(!_isMc &&  _isBitmap)
			{
				var bmd:BitmapData = CommonMethod.drawBitmapData(icon);
				var bmp:Bitmap = new Bitmap(bmd);
				bmp.smoothing = true;
				bmp.x = icon.x;
				bmp.y = icon.y;
				_conIcon.addChildAt(bmp,0);
			}
			else
				_conIcon.addChildAt(icon,0);
		}
	}
}